<?php
/**
 * Created by PhpStorm.
 * User: JSQ-2
 * Date: 2019/2/18
 * Time: 16:29
 */

namespace app\index\controller;

use app\index\base_controller\BaseController;
use app\index\table\FacilityTable;
use think\Db;
use think\Exception;


class Facility2 extends  BaseController {

    public function  facilityList(){

        return view('facility_list');
    }
    /**
 * action--获取信息
 */
    public function getFacilityList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";

        if(empty($sort)){
            $sortStr = 'FACILITY_NO desc';
        }

        $condition = null;

        $listCondition = Login::getListConditionSb();
        if (!empty($listCondition['FACILITY_NO'])) {
            $value = $listCondition['FACILITY_NO'];
            $condition[] = ['FACILITY_NO','like',"$value%"];
        }
        if (!empty($listCondition['FACILITY'])) {
            $value = $listCondition['FACILITY'];
            $condition[] = ['FACILITY','like',"%$value%"];
        }
        if (!empty($listCondition['FACITYPE_ID'])) {
            $value = $listCondition['FACITYPE_ID'];
            $condition[] = ['FACITYPE_ID','=',$value];
        }
        if (!empty($listCondition['DEPART_BELONG'])) {
            $value = $listCondition['DEPART_BELONG'];
            $condition[] = ['DEPART_BELONG','=',$value];
        }
        if (!empty($listCondition['DEPART_USE'])) {
            $value = $listCondition['DEPART_USE'];
            $condition[] = ['DEPART_USE','=',$value];
        }
        if (!empty($listCondition['BELONG_ID'] || $listCondition['BELONG_ID']==='000')) {
            $value = $listCondition['BELONG_ID'];
            $condition[] = ['BELONG_ID','=',$value];
        }
        if (!empty($listCondition['COUNTRY_ID'])) {
            $value = $listCondition['COUNTRY_ID'];
            $condition[] = ['COUNTRY_ID','=',$value];
        }
        if (!empty($listCondition['STATE_ID'])) {
            $value = $listCondition['STATE_ID'];
            $condition[] = ['STATE_ID','=',$value];
        }
        if (!empty($listCondition['PRICE_START'])) {
            $value = $listCondition['PRICE_START'];
            $condition[] = ['PRICE','>=',$value];
        }
        if (!empty($listCondition['PRICE_END'])) {
            $value = $listCondition['PRICE_END'];
            $condition[] = ['PRICE','<=',$value];
        }


        if (!empty($listCondition['ENTERTIME_START'])) {
            $value = Db::raw("to_date('{$listCondition['ENTERTIME_START']}','yyyy-mm-dd')");
            $condition[] = ['ENTERTIME','>=',$value];
        }
        if (!empty($listCondition['ENTERTIME_END'])) {
            $value = Db::raw("to_date('{$listCondition['ENTERTIME_END']}','yyyy-mm-dd')");
            $condition[] = ['ENTERTIME','<=',$value];
        }


        try{
            $listSql = FacilityTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = FacilityTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--编辑
     */
    public function editPage(){
        $facilityNo = input('facilityNo');
        $data = FacilityTable::get($facilityNo);
        $this->assign('data',$data);
//        dump($data);
//        return;

        $this->view->engine->layout('layout_popup');
        return view('facility_edit');
    }


    public function edit(){
        $facilityNo = input('FACILITY_NO');
        $data = input();
        try{
            FacilityTable::update(['FACILITY_NO'=>$facilityNo],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    public function  addpage(){
        $config = FacilityTable::getConfigAdd();
        $data = null;
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('facility_add');
    }
    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('facility_search');
    }

    /**
     * action 新增
     */
    public function add() {
        $data = input();
        try{
            FacilityTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }
}